package study.alex.resumedb.jdo;

import javax.jdo.annotations.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 07.11.13
 * Time: 16:44
 * SVN $Id$
 */


@PersistenceCapable (identityType= IdentityType.APPLICATION, detachable="true")
@Extension(vendorName="DataNucleus", key="mysql-engine-type", value="MyISAM")
public class resumeTracker implements Comparable<resumeTracker> {

    public static final int CONFIRM_CALL_AFTER = 1;
    public static final int NEXT_CALL_AFTER = 7;


    @PrimaryKey
    @Persistent(valueStrategy= IdGeneratorStrategy.INCREMENT)
    private long id;

   @Persistent (defaultFetchGroup = "true")
    private GregorianCalendar sentDate = null;
    @Persistent (defaultFetchGroup = "true")
    private Contact employer = null;
    @Persistent (defaultFetchGroup = "true")
    private GregorianCalendar lastCall = null;
    @Persistent (defaultFetchGroup = "true")
    private GregorianCalendar nextCall = null;
    @Persistent (defaultFetchGroup = "true")
    private GregorianCalendar checkCall = null;
    @Persistent (defaultFetchGroup = "true")
    private GregorianCalendar dateOfInterview = null;
    @Persistent (defaultFetchGroup = "true")
    private Long resumeId = null;
    @Persistent (defaultFetchGroup = "true")
    private resumeStatus status = resumeStatus.UNKNOWN;
    private String comment = "";

    public void setCheckCall(GregorianCalendar checkCall) {
        this.checkCall = checkCall;
    }

    public resumeTracker(Contact employer, GregorianCalendar sentDate, GregorianCalendar checkCall, Long resumeId) {
        this(employer, sentDate, resumeId);
        setCheckCall(checkCall);
    }

    public long getId() {
        return id;
    }

    public resumeTracker(Contact employer, GregorianCalendar sentDate, Long resumeId) {

        this.resumeId = resumeId;
        if (employer != null) {
            this.employer = employer;
            if (sentDate != null) {
                this.sentDate = sentDate;
            }
            else this.sentDate = new GregorianCalendar();
        }
        else this.employer = new Contact();


        nextCall = (GregorianCalendar) (sentDate != null ? sentDate.clone() : null);
        checkCall = (GregorianCalendar) (sentDate != null ? sentDate.clone() : null);
        if (nextCall != null) {
            nextCall.add(GregorianCalendar.DAY_OF_MONTH, NEXT_CALL_AFTER);
        }
        if (checkCall != null) {
            checkCall.add(GregorianCalendar.DAY_OF_MONTH, CONFIRM_CALL_AFTER);
        }
        status = resumeStatus.UNCHECKED;
    }

    public Contact getEmployer() {
        return employer;
    }

    public GregorianCalendar getLastCall() {
        return lastCall;
    }

    public GregorianCalendar getNextCall() {
        return nextCall;
    }

    public String getNextRegularCall() {
        String call =  "";
        switch (status) {
            case UNCHECKED:
                call = DateFormat.getDateInstance(DateFormat.SHORT).format(getCheckCall().getTimeInMillis());
                break;
            case CHECKED:
            case DELAYED:
            case RESERVED:
            case UNKNOWN:
            case CONSIDERED:
                call = DateFormat.getDateInstance(DateFormat.SHORT).format(getNextCall().getTimeInMillis());
                break;
            case REJECTED:
                call = status.toString();
                break;
            case ACCEPTED:
                call = status.toString() + ": " + DateFormat.getDateInstance(DateFormat.SHORT).format(getDateOfInterview().getTimeInMillis());
                break;
        }
        return call;
    }

    public GregorianCalendar getCheckCall() {
        return checkCall;
    }

    public GregorianCalendar getDateOfInterview() {
        return dateOfInterview;
    }

    public Long getResume() {
        return resumeId;
    }

    public resumeStatus getStatus() {
        return status;
    }

    public void setLastCall(GregorianCalendar lastCall) {
        this.lastCall = lastCall;
    }

    public void setDateOfInterview(GregorianCalendar dateOfInterview) {
        this.dateOfInterview = dateOfInterview;
    }

    public void setStatus(resumeStatus status) {
        this.status = status;
    }

    public void setNextCall(GregorianCalendar nextCall) {
        this.nextCall = nextCall;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public GregorianCalendar getSentDate() {
        return sentDate;
    }

    @Override
    public int compareTo(resumeTracker o) {
        return sentDate.compareTo(o.sentDate);
    }
    @Override
    public String toString() {
        return "Отпр: " + DateFormat.getDateInstance(DateFormat.SHORT).format(this.getSentDate().getTimeInMillis()) +
                " Орг: " + this.getEmployer().getOrganization() + " Имя: " + this.getEmployer().getName(); // + " id: " + this.getId();

    }

}
